package com.compass.doctor.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityDiagnoseBinding
import com.compass.doctor.databinding.ActivityVaServiceBinding
import com.compass.doctor.dialog.AppendDiagnoseDialog
import com.compass.doctor.ui.home.adapter.VAServiceAdapter
import com.compass.doctor.ui.home.viewmodel.VAServiceViewModel
import com.compass.doctor.ui.prescription.adapter.DiagnoseAdapter
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.constant.KEY_INDEX
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getColorFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 增值服务
 * Date:2024/7/25 14:05
 */
class VAServiceActivity : BaseVBActivity<ActivityVaServiceBinding>(), OnRefreshListener,
    OnLoadMoreListener {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, VAServiceActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val vaServiceViewModel: VAServiceViewModel by viewModels()

    private lateinit var mAdapter: VAServiceAdapter
    private var mPage = 1
    private var key: String = ""
    private var doctorId: String? = null
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.edtDiagnosis.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                key = mBinding.edtDiagnosis.text.toString().trim()
                showLoading()
                getDiagnosisInfo()
            }
            false
        }

        mBinding.btnSearch.onClick {
            key = mBinding.edtDiagnosis.text.toString().trim()
            showLoading()
            getDiagnosisInfo()
        }
        initRecyclerView()
    }

    override fun initData() {
        doctorId = UserInfoManager.getUserInfo()?.id
        vaServiceViewModel.serviceLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding.refreshLayout.finishRefresh()
                mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding.emptyView.visible()
                } else {
                    mBinding.emptyView.gone()
                }
            } else {
                mBinding.refreshLayout.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            }
        }
        getDiagnosisInfo()
    }

    /**
     * 获取推荐列表数据
     */
    private fun getDiagnosisInfo() {
        showLoading()
        vaServiceViewModel.getServicePatientList(mPage, key, doctorId)

    }


    private fun initRecyclerView() {
        mBinding.refreshLayout.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@VAServiceActivity)
            setOnLoadMoreListener(this@VAServiceActivity)

        }
        mAdapter = VAServiceAdapter()
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)

            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)

            VAServiceDetailsActivity.start(this,item)

        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getDiagnosisInfo()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getDiagnosisInfo()
    }
}